<?php

/**
 * partenaire actions.
 *
 * @package    explications_informatiques
 * @subpackage partenaire
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class partenaireActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $q =  $q = Doctrine_Query::create()
		->from('links')
		->where('online = ?', '1')
		->orderBy('nbentree DESC');
	$this->pager = new sfDoctrinePager(
		'Links',
		10//nb résultats par pages
	  );
	  $this->pager->setQuery($q);
	  $this->pager->setPage($request->getParameter('page', 1));
	  $this->pager->init();
	  $this->linkss = $this->pager->getResults();	
  }
  public function executeClick(sfWebRequest $request)
  {
	  $q = Doctrine_Query::create()
		->from('links')
		->where("idlinks = ?",$request->getParameter('id'));
		$this->link = $q->fetchOne();
		//var_dump($this->link);
		$this->link["nbclick"] += 1;
		$this->link->save();
		$this->redirect($this->link["url"]);
  }

  public function executeNew(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_PARTENAIRE"),"headers","forbidden");
    $this->form = new linksForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_PARTENAIRE"),"headers","forbidden");
    $this->forwardUnless($request->isMethod(sfRequest::POST),"headers","forbidden");

    $this->form = new linksForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("MOD_PARTENAIRE"),"headers","forbidden");
    $this->forward404Unless($links = Doctrine_Core::getTable('links')->find(array($request->getParameter('id'))), sprintf('Object links does not exist (%s).', $request->getParameter('id')));
    $this->form = new linksForm($links);
  }

  public function executeUpdate(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("MOD_PARTENAIRE"),"headers","forbidden");
    $this->forwardUnless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT),"headers","forbidden");
    $this->forward404Unless($links = Doctrine_Core::getTable('links')->find(array($request->getParameter('id'))), sprintf('Object links does not exist (%s).', $request->getParameter('id')));
    $this->form = new linksForm($links);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("SUPPR_PARTENAIRE"),"headers","forbidden");


    $this->forward404Unless($links = Doctrine_Core::getTable('links')->find(array($request->getParameter('idlinks'))), sprintf('Object links does not exist (%s).', $request->getParameter('idlinks')));
    $links->delete();
	if ($cache = $this->getContext()->getViewCacheManager())
	  {
		 $cache->remove('partenaire/index');
	  }
	sfCoreAutoload::logElem("Suppresion d'un partenaire id:".$request->getParameter('idlinks'), $this->getUser()->getGuardUser()->getId(), date("Y-m-d h:i:s"));
	$this->getUser()->setFlash("info","Partenaire supprimé");
    $this->redirect('partenaire/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $links = $form->save();
	  if ($cache = $this->getContext()->getViewCacheManager())
		{
		   $cache->remove('partenaire/index');
		}
	  if($request->getParameter('action') == "create")
	  	$this->getUser()->setFlash("info","Partenaire créé");
	  else
	  	$this->getUser()->setFlash("info","Partenaire modifié");
      $this->redirect('partenaire/index');
    }
  }
}
